package com.define.commons.dao.service.impl;

import com.define.commons.common.utils.PageInfo;
import com.define.commons.dao.dao.BaseDao;
import com.define.commons.dao.service.BaseService;
import com.gitee.fastmybatis.core.exception.QueryException;
import com.gitee.fastmybatis.core.query.Query;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Collections;
import java.util.List;

/**
 * 根据泛型提供数据库操作的 BaseServiceImpl
 * @Author xiewenlong
 * @Date 2018/10/31 12:49
 */
public class BaseServiceImpl<T, I> extends FastMybatisServiceImpl<T, I> implements BaseService<T, I> {

    @Autowired
    private BaseDao<T, I> mapper;

    @Override
    public PageInfo<T> page(Query query){
        try {
            int pages = 0;
            long total = 0L;
            List<T> list = Collections.emptyList();
            if (query.getIsQueryAll()) {
                list = mapper.list(query);
                total = (long)list.size();
                if (total > 0L) {
                    pages = 1;
                }
            } else {
                if (query.getIsSetTotal()) {
                    total = (long)query.getTotal();
                } else {
                    total = mapper.getCount(query);
                }
                if (total > 0L) {
                    list = mapper.list(query);
                    int start = query.getStart();
                    int pageSize = query.getLimit();
                    int pageIndex = start / pageSize + 1;
                    pages = (int)(pageSize == 0 ? 1L : (total + (long)pageSize - 1L) / (long)pageSize);
                }
            }
            return new PageInfo<T>(total, pages, list);
        } catch (Exception e) {
            throw new QueryException(e);
        }
    }

   /* @Override
    public PageInfo<T> page(Query query) {
        PageResult result = null;
        try {
            result = (PageResult)PageInfo.class.newInstance();
        } catch (Exception e) {
            throw new QueryException(e);
        }
        try {
            int pageCount = 0;
            long total = 0L;
            List<T> list = Collections.emptyList();
            if (query.getIsQueryAll()) {
                list = mapper.list(query);
                total = (long)list.size();
                if (total > 0L) {
                    pageCount = 1;
                }
            } else {
                if (query.getIsSetTotal()) {
                    total = (long)query.getTotal();
                } else {
                    total = mapper.getCount(query);
                }
                if (total > 0L) {
                    list = mapper.list(query);
                    int start = query.getStart();
                    int pageSize = query.getLimit();
                    int pageIndex = start / pageSize + 1;
                    result.setStart(start);
                    result.setPageIndex(pageIndex);
                    result.setPageSize(pageSize);
                    pageCount = (int)(pageSize == 0 ? 1L : (total + (long)pageSize - 1L) / (long)pageSize);
                }
            }
            result.setList(list);
            result.setTotal(total);
            result.setPageCount(pageCount);
            return (PageInfo) result;
        } catch (Exception var11) {
            throw new QueryException(var11);
        }
    }*/
}
